<?php
namespace app\api\service;
use app\api\model\bargain;
use app\api\model\bargain_user;
use app\BaseController;
use app\api\model\bargain_user_help;
use think\facade\Db;

class BargainService extends BaseController
{
    /**
     * Author:Su ang
     * 获取砍价活动列表 获取上架的
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function activityTime()
    {

        $data = bargain::with('goods')->where('status','=','1')->select()->toArray();
       return succeedHint($data,'查询成功');

    }

    /**
     * Author:Su ang
     * 用户选择砍价商品 判断第一次砍价
     * @param $u_id
     * @param $b_id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function bargain($u_id,$b_id)
    {
        $wheres = ['uid'=>$u_id,'bargain_id'=>$b_id];
        $data =  bargain_user::where($wheres)->find();
        $data && $data=$data->toarray();
        if (empty($data)){
        return  self::first_bargain($u_id,$b_id);
        }else{
          return succeedHint($data['spread_code'],'已经砍过啦，没有机会了，分享给好友吧');
        }
    }

    /**
     * Author:Su ang
     * 第一刀砍价 95%
     * @param $uid
     * @param $bid
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function first_bargain($uid,$bid)
    {
        $data= bargain::where('id',$bid)->find()->toArray();
        $bargain_price =   $data['bargain_price'];
        $first_bargain = intval( $bargain_price*0.95);
        $price= $bargain_price-$first_bargain;
        $spread_code = rand(111111111,999999999);
        Db::startTrans();
        try{

            bargain_user::create(['uid'=>$uid,'bargain_id'=>$bid,'bargain_price'=>$bargain_price,
                'price'=>$price,'status'=>1,'spread_code'=>$spread_code,'start_time'=>time(),
                'end_time'=>time()+86400]);
            bargain_user_help::create(['uid'=>$uid,'bargain_id'=>$bid,'price'=>$first_bargain,'bargain_user_id'=>$uid]);
            Db::commit();
            //生成订单 状态为砍价中
            //订单价格
            $order_price =   $data['price']-$data['bargain_price'];
        } catch (\Exception $e) {
            Db::rollback();
            return errorHint('未知错误');
        }
        return succeedHint('',"'恭喜你砍价成功砍掉'$first_bargain'元'");
    }

    /**
     * 判断砍价活动是否结束过期
     * Author:Su ang
     * @param $spread_code
     * @param $uid
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function time_bargain($spread_code,$uid)
        {
            $data = bargain_user::where('spread_code',$spread_code)->find()->toArray();
            $end_time = $data['end_time'];
            if (time()<=$end_time){
              return  self::friend_bargain($spread_code,$uid);
            }else{
//                获取商品id
                $bargain_id =   $data['bargain_id'];
               $goods_id = bargain::where('id',$bargain_id)->find()->toArray();
                //生成失败砍价订单
              return  succeedHint('','活动结束');
            }
        }

    /**
     * Author:Su ang
     * 通过口令进行好友砍价
     * @param $spread_code
     * @param $uid
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function friend_bargain($spread_code,$uid)
    {

        Db::startTrans();
        try{
            $data = bargain_user::where('spread_code',$spread_code)->find()->toArray();
            $comment = bargain::where('id',$data['bargain_id'])->find()->toArray();
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return errorHint('未知错误');
        }
        //获取砍价活动id
        $bargain_id = $data['bargain_id'];
        //砍价发起人活动用户id
        $bargain_user_id =  $data['uid'];
        //查询砍价人数条件
        $wheres = ['bargain_user_id'=>$bargain_user_id,'bargain_id'=>$bargain_id];
        $wheresy = ['bargain_user_id'=>$bargain_user_id,'bargain_id'=>$bargain_id,'uid'=>$uid];
        //剩余砍价金额
        $price = $data['price'];
        //剩余砍价人数
        $bargain_num =  $comment['bargain_num']-bargain_user_help::where($wheres)->select()->count() ;
        $is_bargain_num = bargain_user_help::where($wheresy)->select()->count() ;
        //判断当前用户是否已经绑砍
        if ($is_bargain_num==false){
            if ($bargain_num!==1){
                $price_max = intval( $price * 0.6);
                $num = 0.01 + mt_rand() / mt_getrandmax() * ($price_max - 0.01);
                $bargain_price = sprintf("%.2f",$num);  //控制小数后几位
                $priceUpdate = $price - $bargain_price;
                Db::startTrans();
                try{
                    bargain_user_help::create(['uid'=>$uid,'bargain_id'=>$bargain_id,
                        'bargain_user_id'=>$bargain_user_id,'price'=>$bargain_price]);
                    bargain_user::where('spread_code',$spread_code)->update(['price'=>$priceUpdate]);
                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                    return errorHint('未知错误');
                }
                return succeedHint('',"'砍价'.$priceUpdate.'元'") ;
            }else{
                Db::startTrans();
                try{
                    bargain_user_help::create(['uid'=>$uid,'bargain_id'=>$bargain_id,
                        'bargain_user_id'=>$bargain_user_id,'price'=>$price]);
                    bargain_user::where('spread_code',$spread_code)->update(['price'=>0]);
                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                    return errorHint('未知错误');
                }
                //生成砍价成功订单
                //订单价格
                $order_price =   $comment['price']-$comment['bargain_price'];
                return succeedHint('','砍价成功');
            }
        }else{
            return succeedHint('','已经帮过了');
        }




    }

}